package com.lms.logisticsmanagementsystem.controller;

import com.lms.logisticsmanagementsystem.config.TableSplit;
import com.lms.logisticsmanagementsystem.model.*;
import com.lms.logisticsmanagementsystem.service.CarService;
import com.lms.logisticsmanagementsystem.service.OrderService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CarService carService;

    @GetMapping("/order")
    public String orderIndex() {
        return "order/orderInfo";
    }

    @GetMapping("/addOrderInfo")
    public String addOrderInfo(Model model) {
        List<String> cars = carService.getCarInfoByClassAndZT();
        List<Driver> drivers = carService.getDriverInfoByZT();
        model.addAttribute("cars", cars);
        model.addAttribute("drivers", drivers);
        return "order/addOrderInfo";
    }


    @PostMapping("getOrderInfo")
    @ResponseBody
    public Map<String, Object> getOrderInfo(HttpServletRequest req) {
        String otip = req.getParameter("otip");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        Integer p = Integer.valueOf(page);
        Integer l = Integer.valueOf(limit);
        List<Order> list = new ArrayList<>();
        if (otip==null||otip==""){
            list = orderService.getOrderInfo();
        }
        else {
            list=orderService.getOrderInfoByOTip(otip);
        }
        TableSplit tableSplit = new TableSplit();
        Map<String, Object> map = tableSplit.tableFormat(p, l, list);
        return map;
    }

    @PostMapping("/addOrderByBack")
    @ResponseBody
    public String addOrderByBack(@RequestBody OA oa) {
        Date datedown = new Date();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        oa.setDatedown(s.format(datedown));
        oa.setDateout(s.format(datedown));
        /*生成10位订单随机编号*/
        String rand = "";
        for (int i = 0; i < 2; i++) {
            int m = new Random().nextInt(10);
            rand += m;
        }
        s = new SimpleDateFormat("yyyyMMddss");
        oa.setOrderid(s.format(datedown) + rand);
        /*生成12位配送单随机编号*/
        String prand = "";
        for (int i = 0; i < 2; i++) {
            int m = new Random().nextInt(10);
            prand += m;
        }
        s = new SimpleDateFormat("ssddMMyyyy");
        oa.setPsbh(s.format(datedown) + rand);
        /*整理订单数据和配送单数据*/
        //获取车辆信息
        Car car = carService.getCarInfoByCBH(oa.getCarCheck());
        //获取司机信息
        Driver driver = carService.getDriverInfoById(Integer.valueOf(oa.getDriverCheck()));
        //订单
        Order order = new Order();
        order.setSendername(oa.getSendername());
        order.setSenderphone(oa.getSenderphone());
        order.setOrderid(oa.getOrderid());
        order.setSenderaddr(oa.getSenderaddr());
        order.setQujianaddr(oa.getQujianaddr());
        order.setQujianaddrdtl(oa.getQujianaddrdtl());
        order.setHwmc(oa.getHwmc());
        order.setHwzl(oa.getHwzl());
        order.setrname(oa.getRname());
        order.setReaddr(oa.getReaddr());
        order.setReaddrdtl(oa.getReaddrdtl());
        order.setRephone(oa.getRephone());
        order.setDatedown(oa.getDatedown());
        order.setDateout(oa.getDateout());
        order.setStartaddr(oa.getStartaddr());
        order.setEndaddr(oa.getEndaddr());
        order.setPrice(oa.getPrice());
        order.setOrderclass(oa.getOrderclass());
        order.setOrderstatus(oa.getOrderstatus());
        order.setAttention(oa.getAttention());
        //配送单
        PS ps = new PS();
        ps.setPsbh(oa.getPsbh());
        ps.setJjrxm(oa.getSendername());
        ps.setSjrxm(oa.getRname());
        ps.setSjrdh(oa.getRephone());
        ps.setDdlb(oa.getOrderclass());
        ps.setSaddr(oa.getSenderaddr());
        ps.setEaddr(oa.getReaddr());
        ps.setPswp(oa.getHwmc());
        ps.setClbh(oa.getCarCheck());
        ps.setCphm(car.getCHM());
        ps.setSjxm(driver.getXM());
        ps.setSjdh(driver.getSJH());
        ps.setPstime(oa.getDateout());
        ps.setZt("运输中");
        /*更新车辆信息和驾驶员信息*/
        //车辆信息
        String rwmc = "送" + oa.getHwmc() + "至" + oa.getEndaddr();
        String rwrq = oa.getDatedown();
        String shdd = oa.getReaddr() + oa.getReaddrdtl();
        String hwmc = oa.getHwmc();
        String bz = oa.getAttention();
        String zt = "使用中";
        car.setRWMC(rwmc);
        car.setRWRQ(rwrq);
        car.setSHDD(shdd);
        car.setHWMC(hwmc);
        car.setBZ(bz);
        car.setZT(zt);
        //司机信息
        driver.setZT("忙碌");
        /*插入数据*/
        boolean cf = carService.updateCarInfoByCBH(car);
        boolean df = carService.updateDriverStatusById(driver);
        boolean flag = orderService.addOrderInfo(order);
        boolean pf = orderService.addPSInfo(ps);
        if (flag && pf && cf && df) {
            return "success";
        } else return "fail";
    }

    @GetMapping("/editOrderInfo")
    public String editOrderInfo(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        //获取编号id相关信息
        Order order = orderService.getOrderInfoById(Integer.valueOf(id));
        model.addAttribute("order", order);
        //分割地址获取省市区
        //寄件人
        String saddr = order.getSenderaddr();
        String[] s = saddr.split(" ");
        //取件人
        String qaddr = order.getQujianaddr();
        String[] q = qaddr.split(" ");
        //收件人
        String raddr = order.getReaddr();
        String[] r = raddr.split(" ");
        model.addAttribute("s", s);
        model.addAttribute("q", q);
        model.addAttribute("r", r);
        return "order/editOrderInfo";
    }

    @PostMapping("/editOrder")
    @ResponseBody
    public String editOrder(@RequestBody Order order) {
        if (order == null) {
            return "info-error";
        }
        //更新数据
        boolean flag = orderService.updateOrderInfoById(order);
        if (flag) {
            return "success";
        } else {
            return "fail";
        }
    }

    @GetMapping("/orderInfoDtl")
    public String orderInfoDtl(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        //获取编号id相关信息
        Order order = orderService.getOrderInfoById(Integer.valueOf(id));
        model.addAttribute("order", order);
        //分割地址获取省市区
        //寄件人
        String saddr = order.getSenderaddr();
        String[] s = saddr.split(" ");
        //取件人
        String qaddr = order.getQujianaddr();
        String[] q = qaddr.split(" ");
        //收件人
        String raddr = order.getReaddr();
        String[] r = raddr.split(" ");
        model.addAttribute("s", s);
        model.addAttribute("q", q);
        model.addAttribute("r", r);
        return "order/orderInfoDtl";
    }

    @PostMapping("/generateOrder")
    @ResponseBody
    public String generateOrder(HttpServletRequest request) {
        String id = request.getParameter("id");
        //更新订单状态
        boolean flag = orderService.generateOrder(Integer.valueOf(id));
        if (flag) {
            return "success";
        } else {
            return "fail";
        }
    }

    @PostMapping("/getCarBHAndZZ")
    @ResponseBody
    public Map<String, Object> getCarBHAndZZ(@RequestParam("clx") String clx) {
        Map<String, Object> map = new HashMap<>();
        List<Car> carList = carService.getCarBHByCLX(clx);
        map.put("carlist", carList);
        return map;
    }

    @PostMapping("delOrderInfo")
    @ResponseBody
    public String delOrderInfo(@RequestParam("id") String id) {
        Integer oid = Integer.valueOf(id);
        boolean flag = orderService.delOrderInfoById(oid);
        if (flag) {
            return "success";
        } else {
            return "fail";
        }
    }

    /*
        配送信息
     */
    @GetMapping("/psInfo")
    public String psInfo() {
        return "order/psInfo";
    }

    @PostMapping("getPSInfo")
    @ResponseBody
    public Map<String, Object> getPSInfo(HttpServletRequest req) {
        String pstip = req.getParameter("pstip");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        Integer p = Integer.valueOf(page);
        Integer l = Integer.valueOf(limit);
        List<PS> list = new ArrayList<>();
        if (pstip == null || pstip == "") {
            list = orderService.getPSInfo();
        } else {
            list = orderService.getPSInfoByPSTip(pstip);
        }
        TableSplit tableSplit = new TableSplit();
        Map<String, Object> map = tableSplit.tableFormat(p, l, list);
        return map;
    }

    @PostMapping("/shPS")
    @ResponseBody
    public String shPS(HttpServletRequest request) {
        String psbh = request.getParameter("psbh");
        String clbh = request.getParameter("clbh");
        String sjxm = request.getParameter("sjxm");
        String sjdh = request.getParameter("sjdh");
        //获取送达时间
        Date date = new Date();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String shtime = s.format(date);
        //修改配送单
        boolean p = orderService.updatePSInfoStatusByPSBH(psbh, shtime);
        //修改车辆信息
        boolean c = carService.updateCarStatusByCBH(clbh);
        //修改司机信息
        boolean d = carService.updateDriverStatusByPhoneAndXM(sjxm, sjdh);
        if (p && c && d) {
            return "success";
        } else {
            String mes = "";
            if (!p) {
                mes += "p";
            }
            if ((!c)) {
                mes += "c";
            }
            if (!d) {
                mes += "d";
            }
            return mes;
        }
    }

    @PostMapping("/delPSInfo")
    @ResponseBody
    public String delPSInfoByPSBH(@RequestParam("psbh") String psbh) {
        boolean flag = orderService.delPSInfoByPSBH(psbh);
        if (flag) {
            return "success";
        } else return "fail";
    }
}
